var app = getApp()
var myVar = null
Page({
  data: {
    orderList: [],
    page: 1,
    pagesize: 5,
    hasMoreData: true,
    member_id:0,
    isInnerBack:false
  },
  onLoad: function (options) {
    if (options.isHome == 1){
      this.setData({
        isInnerBack: false
      })
    }
  },
  onShow: function () {
    // if (!this.data.isInnerBack){
      this.setData({
        page: 1
      })
    // }
    clearTimeout(myVar)
    this.getToken()
  },
  onHide: function(){
    clearTimeout(myVar)
  },
  onUnload:function(){
    clearTimeout(myVar)
  },
  loadData() {
    let self = this
    app.request({
      url: '/home/order/getList',
      method: 'POST',
      data: {
        member_id: self.data.member_id,
        page: self.data.page,
        pagesize: self.data.pagesize
      },
      success: (res) => {
        var listTemp = self.data.orderList
        if (self.data.page == 1) {
          listTemp = []
        }
        if (self.data.page == 1 && res.result == null) {
          self.setData({
            dataList: []
          })
        }
        if (res.result != null && res.result.length > 0) {
          var list = res.result
          for(var s = 0;s<res.result.length;s++){
            res.result[s].checkin_time = self.getDateText(res.result[s].checkin_time)
            res.result[s].checkout_time = self.getDateText(res.result[s].checkout_time)
          }
          if (list.length < self.data.pagesize) {
            self.setData({
              orderList: listTemp.concat(list),
              hasMoreData: false
            })
          } else {
            self.setData({
              orderList: listTemp.concat(list),
              hasMoreData: true,
              page: self.data.page + 1
            })
          }
          for (var i = 0; i < self.data.orderList.length; i++) {
            var endtime = self.data.orderList[i].down_time
            //var endtime = self.data.orderList[i].down_time - 1340
            var isShowed = self.data.orderList[i].endtime
            if (endtime > 0 && isShowed == undefined) {
              self.listcountdown(self, endtime, i)
            }
          }
        }
      }
    })
  },
  //下拉
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.loadData()
    wx.stopPullDownRefresh();
  },
  //上拉
  onReachBottom: function () {
    console.log(this.data.hasMoreData)
    if (this.data.hasMoreData) {
      this.loadData()
    } else {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
    }
  },
  getToken() {
    let self = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        //console.log(res)
        var member_id = res.data.member_id
        var openid = res.data.openid
        self.setData({
          member_id: member_id
        })
        self.loadData()
      },
      fail: (res) => {
        wx.navigateTo({
          url: '/page/component/login/index',
        })
      }
    })
  },
  //倒计时方法
  listcountdown(that, end_time, param) {
    var currentList = that.data.orderList;
    //console.log(end_time)
    currentList[param].endtime = this.formatSeconds(end_time);
    if (end_time < 0) {
      console.log("记时完成，刷新本页")
      //时间到刷新
      that.setData({
        isInnerBack: false
      })
      that.onShow()
      currentList[param].endtime = "已结束"
      return;
    }
    that.setData({
      orderList: currentList
    })
    myVar = setTimeout(function () {
      that.listcountdown(that, end_time - 1, param);
    }, 1000)
  },
  // 时间格式化输出 timeout
  formatSeconds(value) {
    var theTime = parseInt(value);// 秒 
    var theTime1 = 0;// 分 
    var theTime2 = 0;// 小时 
    // alert(theTime); 
    if(theTime > 60) {
      theTime1 = parseInt(theTime / 60);
      theTime = parseInt(theTime % 60);
      // alert(theTime1+"-"+theTime); 
      if (theTime1 > 60) {
        theTime2 = parseInt(theTime1 / 60);
        theTime1 = parseInt(theTime1 % 60);
      }
    }
    var result = "" + parseInt(theTime) + "秒";
    if (theTime1 > 0) {
      result = "" + parseInt(theTime1) + "分" + result;
    }
    if (theTime2 > 0) {
      result = "" + parseInt(theTime2) + "小时" + result;
    }
    return result; 
  },
  getDateText(string) {
    var date = new Date(string);
    return (date.getMonth() + 1) + "月" + date.getDate() + "日"
  }
})